package bdhub.littletail.lib.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 *
 * Created by FreddyChen on 16/2/23.
 * chenshichao@outlook.com
 */
public class DateUtils {
    
	private static final long INTERVAL_MINUTE = 60 * 1000;
	private static final long INTERVAL_HOUR = 60 * INTERVAL_MINUTE;
	private static final long INTERVAL_DAY = 24 * INTERVAL_HOUR;
	
	//容差值 1 分钟
	private static final long DELTA_TIME = 1 * 60 * 1000;

    
	private static final Calendar mCalendarCache1 = Calendar.getInstance(); //仅用于cache
	private static final Calendar mCalendarCache2 = Calendar.getInstance();
	
	/**
	 * 获取列表的时间描述
	 *
	 * @param timeMillis
	 * @return
	 */
	public static String getListDateName(long timeMillis) {
		Date now = new Date();
		Date date;
		if (0 == timeMillis) {
			date = now;
		} else {
			date = new Date(timeMillis);
		}
		
		// SimpleDateFormat不能满足性能，自己实现
		
		Calendar nowCalendar = mCalendarCache1;
		nowCalendar.setTime(now);
		int nowSecond = nowCalendar.get(Calendar.SECOND);
		int nowMinute = nowCalendar.get(Calendar.MINUTE);
		int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);
		//int nowDate = nowCalendar.get(Calendar.DATE);
		//int nowMonth = nowCalendar.get(Calendar.MONTH) + 1;
		int nowYear = nowCalendar.get(Calendar.YEAR);
		
	    Calendar inputCalendar = mCalendarCache2;
	    inputCalendar.setTime(date);
	    int inputSecond = inputCalendar.get(Calendar.SECOND);
		int inputMinute = inputCalendar.get(Calendar.MINUTE);
		int inputHour = inputCalendar.get(Calendar.HOUR_OF_DAY);
		int inputDate = inputCalendar.get(Calendar.DATE);
		int inputMonth = inputCalendar.get(Calendar.MONTH) + 1;
		int inputYear = inputCalendar.get(Calendar.YEAR);
		
		long interval = now.getTime() - date.getTime();
//		[凌晨|早上|中午|下午|晚上] HH:mm
//		昨天
//		m月d日
//		yyyy年m月d日
		StringBuilder result = new StringBuilder();
		if (interval >= -DELTA_TIME) {
			long dateDiffSecond = interval / 1000 - ( ((nowHour * 60 + nowMinute) * 60 + nowSecond) -  ((inputHour * 60 + inputMinute) * 60 + inputSecond) ); //相对值，单位秒
			if (dateDiffSecond < 0 + DELTA_TIME) {
				//今天
				result.append(getDayTimeDesc(inputHour));
				result.append(fillZero(inputHour));
				result.append(":");
				result.append(fillZero(inputMinute));
			} else if (dateDiffSecond < INTERVAL_DAY / 1000 + DELTA_TIME) {
				// 昨天
				result.append("昨天 ");
			} else if (inputYear == nowYear) {
				// 今年，
				result.append(fillZero(inputMonth));
				result.append("月");
				result.append(fillZero(inputDate));
				result.append("日 ");
			} else {
				result.append(inputYear);
				result.append("年");
				result.append(fillZero(inputMonth));
				result.append("月");
				result.append(fillZero(inputDate));
				result.append("日 ");
			}
		} else {
			result.append(inputYear);
			result.append("年");
			result.append(fillZero(inputMonth));
			result.append("月");
			result.append(fillZero(inputDate));
			result.append("日 ");
		}
		return result.toString();
	}
	
	/**
	 * 获取聊天窗口的时间描述
	 *
	 * @param time
	 * @return
	 */
	public static String getChatDateName(long time) {
		Date now = new Date();
		Date date = new Date(time);
		
		// SimpleDateFormat不能满足性能，自己实现
		
		Calendar nowCalendar = mCalendarCache1;
		nowCalendar.setTime(now);
		int nowSecond = nowCalendar.get(Calendar.SECOND);
		int nowMinute = nowCalendar.get(Calendar.MINUTE);
		int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);
		//int nowDate = nowCalendar.get(Calendar.DATE);
		//int nowMonth = nowCalendar.get(Calendar.MONTH) + 1;
		int nowYear = nowCalendar.get(Calendar.YEAR);
		
		Calendar inputCalendar = mCalendarCache2;
		inputCalendar.setTime(date);
		int inputSecond = inputCalendar.get(Calendar.SECOND);
		int inputMinute = inputCalendar.get(Calendar.MINUTE);
		int inputHour = inputCalendar.get(Calendar.HOUR_OF_DAY);
		int inputDate = inputCalendar.get(Calendar.DATE);
		int inputMonth = inputCalendar.get(Calendar.MONTH) + 1;
		int inputYear = inputCalendar.get(Calendar.YEAR);
		
		long interval = now.getTime() - date.getTime();
//		[凌晨|早上|中午|下午|晚上] HH:mm
//		昨天 [凌晨|早上|中午|下午|晚上] HH:mm
//		m月d日 [凌晨|早上|中午|下午|晚上] HH:mm
//		yyyy年m月d日 [凌晨|早上|中午|下午|晚上] HH:mm
		StringBuilder result = new StringBuilder();
		if (interval >= -DELTA_TIME) {
			long dateDiffSecond = interval / 1000 - ( ((nowHour * 60 + nowMinute) * 60 + nowSecond) -  ((inputHour * 60 + inputMinute) * 60 + inputSecond) ); //相对值，单位秒
			if (dateDiffSecond < 0 + DELTA_TIME) {
				//今天
			} else if (dateDiffSecond < INTERVAL_DAY / 1000 + DELTA_TIME) {
				// 昨天
				result.append("昨天 ");
			} else if (inputYear == nowYear) {
				// 今年，
				result.append(fillZero(inputMonth));
				result.append("月");
				result.append(fillZero(inputDate));
				result.append("日 ");
			} else {
				result.append(inputYear);
				result.append("年");
				result.append(fillZero(inputMonth));
				result.append("月");
				result.append(fillZero(inputDate));
				result.append("日 ");
			}
			result.append(getDayTimeDesc(inputHour));
			result.append(fillZero(inputHour));
			result.append(":");
			result.append(fillZero(inputMinute));
		} else {
			result.append(inputYear);
			result.append("年");
			result.append(fillZero(inputMonth));
			result.append("月");
			result.append(fillZero(inputDate));
			result.append("日 ");
		}
		return result.toString();
	}
	
	/**
	 * 当天时间的描述<br />
	 * 0:00-5:59 凌晨
	 * 6:00-11:59 早上
	 * 12:00-12:59 中午
	 * 13:00-17:59 下午
	 * 18:00-23:59 晚上
	 *
	 * @return [凌晨|早上|中午|下午|晚上]
	 */
	private static String getDayTimeDesc(int inputHour) {
		if (inputHour < 6) {
			return "凌晨";
		} else if (inputHour < 12) {
			return "早上";
		} else if (inputHour < 13) {
			return "中午";
		} else if (inputHour < 18) {
			return "下午";
		} else {
			return "晚上";
		}
	}
	
	/**
	 * 获取时间的缩略形式
	 * @param date
	 * @return
	 */
	public static String getShortDateName(Date date) {
		Date now = new Date();
		//某些时候，date似乎会为null，所以这里做一个保护
		if(date == null){
			date = now;
		}
		
		// SimpleDateFormat不能满足性能，自己实现
		
		Calendar nowCalendar = mCalendarCache1;
		nowCalendar.setTime(now);
		int nowSecond = nowCalendar.get(Calendar.SECOND);
		int nowMinute = nowCalendar.get(Calendar.MINUTE);
		int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);
		int nowDate = nowCalendar.get(Calendar.DATE);
		int nowMonth = nowCalendar.get(Calendar.MONTH) + 1;
		int nowYear = nowCalendar.get(Calendar.YEAR);
		
		Calendar inputCalendar = mCalendarCache2;
		inputCalendar.setTime(date);
		int inputSecond = inputCalendar.get(Calendar.SECOND);
		int inputMinute = inputCalendar.get(Calendar.MINUTE);
		int inputHour = inputCalendar.get(Calendar.HOUR_OF_DAY);
		int inputDate = inputCalendar.get(Calendar.DATE);
		int inputMonth = inputCalendar.get(Calendar.MONTH) + 1;
		int inputYear = inputCalendar.get(Calendar.YEAR);
		
		long interval = now.getTime() - date.getTime();
		// 允许系统时间误差1分钟
		if (interval + INTERVAL_MINUTE < 0) {
			//return sf_y_m_d.format(date);
		} else if (interval < INTERVAL_MINUTE) {
			return "刚刚";
		} else if (interval < INTERVAL_HOUR) {
			return (interval / INTERVAL_MINUTE) + "分钟前";
		}
		
		if (interval >= 0) {
			long dateDiffSecond = interval / 1000 - ( ((nowHour * 60 + nowMinute) * 60 + nowSecond) -  ((inputHour * 60 + inputMinute) * 60 + inputSecond) ); //相对值，单位秒
			if (dateDiffSecond < 0 + 1) { //误差1秒
				// 今天 "HH:mm"
				return fillZero(inputHour) + ":" + fillZero(inputMinute);
			} else if (dateDiffSecond < INTERVAL_DAY / 1000 + 1) {
				// 昨天 "昨天 HH:mm"
				return "昨天 " + fillZero(inputHour) + ":" + fillZero(inputMinute);
				
			} else if (dateDiffSecond < INTERVAL_DAY / 1000 * 2 + 1) {
				// 前天 "前天 HH:mm"
				return "前天 " + fillZero(inputHour) + ":" + fillZero(inputMinute);
			}
			if (inputYear == nowYear) {
				//今年，"MM月dd日"
				return fillZero(inputMonth) + "月" + fillZero(inputDate) + "日";
			}
		}
		//其他
		return inputYear + "/" + fillZero(inputMonth) + "/" + fillZero(inputDate);
		
	}
	
	private static String fillZero(int value) {
		return (value < 10 ? "0" : "") + value;
	}
	
	
    /**
     * 指定格式返回当前系统时间
     */
    public static String getDataTime(String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date());
    }

    /**
     * 返回当前系统时间(格式以HH:mm形式)
     */
    public static String getDataTime() {
        return getDataTime("HH:mm");
    }
    
    public static int getAge(Date birthday) {
    	Calendar cal = Calendar.getInstance();
		if (cal.before(birthday)) {
			return 0;
		}
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH);
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

		cal.setTime(birthday);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;

		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else {
				age--;
			}
		}

		return age;
    	
//    	if (null == birthday) {
//			return 0;
//		}
//    	long i= (System.currentTimeMillis() - birthday.getTime())/(1000*60*60*24); 
//		return Math.min(100, (int) (i/365));
    }
}
